package com.sgb.kjwl.viewmodel.supplier3;

import androidx.databinding.ObservableField;
import android.util.Log;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.supplier3.SupplierRecommendEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.material.SupplierFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class SupplierFrgModel extends BaseViewModel {
    private SupplierFragment fragment;
    private HomeRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public ObservableField<GoodsInfoTypeEntity3> typeData = new ObservableField<>(new GoodsInfoTypeEntity3());
    public ObservableField<GoodsInfoTypeEntity3> materialData = new ObservableField<>(new GoodsInfoTypeEntity3());

    public SupplierFrgModel(SupplierFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

//    //获取供货商列表数据
//    public void getSupplierListModel(Map map, boolean isLoadMore) {
//        page = isLoadMore ? page + 1 : 1;
//        map.put("areaCode", repertory.getAreaCode());
//        map.put("pageNum", page);
//        map.put("pageSize", size);
//        repertory.getSupplierMapData(map)
//                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierEntity3>>>>() {
//                    @Override
//                    public void accept(BaseEntity<ListResponse<List<SupplierEntity3>>> listResponseBaseEntity) throws Exception {
//                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            activity3.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

    //获取供货商列表数据
    public void getSupplierListModel(Map map, boolean isLoadMore) {
        Log.e("tagmap", map.toString());
        page = isLoadMore ? page + 1 : 1;
        map.put("areaCode", repertory.getAreaCode());
        map.put("page", page);
        map.put("limit", size);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getSupplierList(map)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SupplierRecommendEntity>>> listResponseBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (listResponseBaseEntity==null){
                            fragment.showList(new ArrayList<>(), isLoadMore);
                            return;
                        }
                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }


    public void getDistanceModel() {
        repertory.getDistance()
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.showDistancePopWindow(listBaseEntity.getData());
                            fragment.showTypePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }


//    public void getSupplierCityModel() {
//        repertory.getSupplierCityData(repertory.getAreaCode())
//                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showCityPopWindow(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

//    public void getSupplierDistanceModel() {
//        repertory.getSupplierDistanceData()
//                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            activity3.showDistancePopWindow(listBaseEntity.getData());
//                            activity3.showTypePopWindow(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

//    public void getSupplierSortModel() {
//        repertory.getSupplierSortData()
//                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showSortPopWindow(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

    //获取供货商地图数据
    public void getSupplierMapModel(Map map) {
        Log.e("tag地图数据map", map.toString());
        map.put("current", page);
        map.put("limit", 10);
        repertory.getSupplierMapData(map)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SupplierRecommendEntity>>> listResponseBaseEntity) throws Exception {
                        if (listResponseBaseEntity==null||listResponseBaseEntity.getData()==null){
                            fragment.showMap(new ArrayList<>());
                            return;
                        }
                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.showMap(listResponseBaseEntity.getData().getList());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });

    }

    /**
     * 获取经纬度
     */
//    public void getPickupLocationModel() {
//        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), null, null, null).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
//                if (response.body() == null) return;
//                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                    fragment.showLocation(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
//            }
//        });
//    }


}
